package cn.dwy2002.binaryhandwritingserver.service.impl;

import cn.dwy2002.binaryhandwritingserver.common.Result;
import cn.dwy2002.binaryhandwritingserver.common.utils.JwtUtil;
import cn.dwy2002.binaryhandwritingserver.mapper.UsersMapper;
import cn.dwy2002.binaryhandwritingserver.pojo.dto.LoginDTO;
import cn.dwy2002.binaryhandwritingserver.pojo.po.Users;
import cn.dwy2002.binaryhandwritingserver.service.service.AdminService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Objects;

import static cn.dwy2002.binaryhandwritingserver.common.Result.fail400;
import static cn.dwy2002.binaryhandwritingserver.common.Result.success200;
import static cn.dwy2002.binaryhandwritingserver.common.utils.Utils.isEmptyOrNull;


/**
 * 用户管理
 *
 * @author 杜伟毅
 * @version 1.0
 * @since 2025/05/29
 */
@Service
@RequiredArgsConstructor
public class AdminServiceImpl implements AdminService {
    private final UsersMapper mapper;
    private final JwtUtil jwtUtil;

    /**
     * 用户登录验证
     *
     * @param dto 登录信息dto
     * @return 认证成功的用户对象
     */
    @Override
    public Result<String> login(LoginDTO dto) {
        if (isEmptyOrNull(dto.getUsername())) return fail400("用户名为空");
        if (isEmptyOrNull(dto.getPassword())) return fail400("密码为空");
        Users users = mapper.selectByUsernameAndPassword(dto.getUsername(), dto.getPassword());
        if (Objects.isNull(users)) return fail400("登陆失败");
        return success200("登陆成功", jwtUtil.createToken(users));
    }

    /**
     * 更新用户信息
     *
     * @param user 用户对象（必须包含有效ID）
     * @return 更新后的用户对象
     */
    @Override
    public Result<Void> updateUser(Users user) {
        return null;
    }
}
